package cn.lixiaodan.hqlbuilder.gen;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.lixiaodan.hqlbuilder.model.ClassModel;

public class JavaHqlGenerator extends HqlGenerator {
	public static final Pattern ptn = Pattern.compile("^(\t?)([^\\/\n]*)$", Pattern.MULTILINE);

	@Override
	public String make(ClassModel[] cmarr, String[] conditions) {
		String hql = super.make(cmarr, conditions);
		StringBuffer sb = new StringBuffer();
		sb.append("private static final String ListHql = ");
		Matcher mch = ptn.matcher(hql);
		// 最后起始位置
		int lastend = 0;
		while (mch.find()) {
			String block1 = mch.group(1);
			String block2 = mch.group(2);
			int start = mch.start();
			int end = mch.end();

			if (start > lastend) {
				sb.append(hql.substring(lastend, start));
			}
			sb.append(block1);
			sb.append(lastend == 0 ? "\"" : "+ \"");
			sb.append(block2);
			sb.append("\"//");
			lastend = end;
		}
		if (lastend < hql.length()) {
			sb.append(hql.substring(lastend));
		}
		sb.append(";");
		
		return sb.toString();
	}
}
